#include "MemberDialog.h"

MemberDialog::MemberDialog()
{
    row = -1;

    layout_main = new QVBoxLayout;   //功能布局
    layout_column = new QHBoxLayout;
    list_members = new QListWidget();    //成员列表
    button_delete = new QPushButton();
    label_list = new QVector<QLabel*>;

    list_id = new QVector<QString>;
    list_name = new QVector<QString>;
    list_create_time = new QVector<QString>;
    list_subscrib_count = new QVector<QString>;
}


MemberDialog::~MemberDialog()
{
    delete list_subscrib_count;
    delete list_create_time;
    delete list_name;
    delete list_id;

    for (QVector<QLabel*>::const_iterator iter = label_list->begin();iter != label_list->end(); iter++)
    {
        delete *iter;
    }
    delete label_list;

    delete button_delete;
    delete list_members;
    delete layout_main;

}

void MemberDialog::init_view_main()
{
    setFixedSize(480, 360);
    setWindowTitle("成员管理");

    layout_main->setSpacing(10);    //设置控件距离
    setLayout(layout_main);

    for(int i = 0; i < 3; i++)
    {
        QLabel *label = new QLabel();
        label_list->push_back(label);
    }

    layout_column->addStretch(1);
    (*label_list)[0]->setText("用户名");
    (*label_list)[0]->setStyleSheet("QLabel{font:10px;}");
    layout_column->addWidget((*label_list)[0], 18);
    (*label_list)[1]->setText("创建时间");
    (*label_list)[1]->setStyleSheet("QLabel{font:10px;}");
    layout_column->addWidget((*label_list)[1], 12);
    (*label_list)[2]->setText("订阅数");
    (*label_list)[2]->setStyleSheet("QLabel{font:10px;}");
    layout_column->addWidget((*label_list)[2], 6);
    layout_main->addLayout(layout_column);

    layout_main->addWidget(list_members);
    reflash_tabel();

    button_delete->setText("删除");
    button_delete->setStyleSheet("QLabel{font:15px;padding: 5px 15px 5px 15px;}");
    QObject::connect(button_delete,&QPushButton::clicked,button_delete,[&](){
        if(row != -1)
        {
            SQLHelper sql_helper;
            sql_helper.connect();

            if((*list_subscrib_count)[row] == "0")
            {
                QString user_id = (*list_id)[row];
                sql_helper.delete_table("user", "id='" + user_id + "';");
                setWindowTitle("删除成功");
            }
            else
            {
                setWindowTitle("删除失败（无法删除订阅用户）");
            }

            reflash_tabel();
        }
    }); //设置监听
    layout_main->addWidget(button_delete);
}

void MemberDialog::reflash_tabel()
{
    SQLHelper sql_helper;
    sql_helper.connect();


    QString sql1 = "SELECT user.id, user.name, user.create_time, count(*) FROM user RIGHT JOIN subscribe on user.id = subscribe.user_id GROUP BY user.id;";
    QString sql2 = "SELECT user.id, user.name, user.create_time FROM user WHERE (SELECT count(1) AS num FROM subscribe WHERE subscribe.user_id = user.id) = 0;";

    QSqlQuery* query1 = sql_helper.exec_sql(sql1);  //订阅者
    QSqlQuery* query2 = sql_helper.exec_sql(sql2);  //非订阅者

    int count = 0;
    list_id->clear();
    list_name->clear();
    list_create_time->clear();
    list_subscrib_count->clear();

    while (query1->next()) {
        list_id->push_back(query1->value(0).toString());
        list_name->push_back(query1->value(1).toString());
        list_create_time->push_back(query1->value(2).toString());
        list_subscrib_count->push_back(query1->value(3).toString());
        count++;
    }

    while (query2->next()) {
        if(query2->value(0).toString() == "0")  //忽略管理员
        {
            continue;
        }
        list_id->push_back(query2->value(0).toString());
        list_name->push_back(query2->value(1).toString());
        list_create_time->push_back(query2->value(2).toString());
        list_subscrib_count->push_back("0");
        count++;
    }

    list_members->clear();
    for(int i = 0; i < count; i++)
    {
        QListWidgetItem *item=new QListWidgetItem(list_members);  // item
        QWidget *widget = new QWidget(list_members);  // item视图
        item->setSizeHint(QSize(120, 60));  // item大小
        QHBoxLayout *layout_item = new QHBoxLayout(widget); // item布局

        QLabel *label_user_name = new QLabel(widget);
        label_user_name->setText((*list_name)[i]);
        label_user_name->setStyleSheet("QLabel{font:10px;}");

        QLabel *label_create_time = new QLabel(widget);
        label_create_time->setText((*list_create_time)[i]);
        label_create_time->setStyleSheet("QLabel{font:10px;}");

        QLabel *label_subscrib_count = new QLabel(widget);
        label_subscrib_count->setText((*list_subscrib_count)[i]);
        label_subscrib_count->setStyleSheet("QLabel{font:10px;}");


        layout_item->addWidget(label_user_name, 9);
        layout_item->addWidget(label_create_time, 6);
        layout_item->addWidget(label_subscrib_count, 3);
        if((*list_subscrib_count)[i] != "0")
        {
            widget->setToolTip("订阅用户");
        }
        else
        {
            widget->setToolTip("非订阅用户");
        }

        widget->setLayout(layout_item);

        list_members->setItemWidget(item,widget);
    }

    QObject::connect(list_members,&QListWidget::itemClicked,list_members,[&]{
        row = list_members->currentRow(); // 选中的item
    });

    list_members->update();
}
